package com.game.data;

import com.game.config.Coefficient;
import com.game.model.BaseProperty;
import com.game.model.SpecialProperty;
import com.game.model.api.EquipType;
import com.game.model.goods.Goods;
import com.game.model.api.GoodsType;
import com.game.model.api.Quality;
import com.game.model.equip.PlayerEquip;
import com.game.model.goods.BaseGoods;
import com.game.model.goods.ExpGoods;
import com.game.utils.Format;
import com.game.utils.JDBCUtil;
import com.game.view.TranslateToChinese;

import java.sql.ResultSet;
import java.util.*;

/**
 * @Description:物品数据
 **/
public class GoodsData {
    public static Goods getGoods(int id) {
        ArrayList<Goods> goods = new ArrayList<>();
        goods.addAll(baseGoods());
        goods.addAll(playerEquips());
        for (Goods g : goods) {
            if (g.getId() == id) {
                return g;
            }
        }

        return null;
    }

    public static List<BaseGoods> baseGoods() {
        List<BaseGoods> baseGoodsList = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            //经验
            baseGoodsList.add(new BaseGoods(1, GoodsType.Exp, "经验", Quality.WHITE, 0, "用于升级角色的经验"));
        } else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from base_goods");
                while (rs.next()) {
                    BaseGoods baseGoods = new BaseGoods();
                    baseGoods.setId(rs.getInt("id"));
                    baseGoods.setName(rs.getString("name"));
                    baseGoods.setType(Format.getGoodsType(rs.getInt("type")));
                    baseGoods.setQuality(Format.getQuality(rs.getInt("quality")));
                    baseGoods.setUseLevel(rs.getInt("use_level"));
                    baseGoods.setDescription(rs.getString("description"));
                    baseGoodsList.add(baseGoods);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return baseGoodsList;
    }

    public static List<PlayerEquip> playerEquips() {
        ArrayList<PlayerEquip> playerEquips = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {

        } else if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from player_equip");
                while (rs.next()) {
                    PlayerEquip playerEquip = new PlayerEquip();
                    playerEquip.setId(rs.getInt("id"));
                    playerEquip.setName(rs.getString("name"));
                    playerEquip.setType(Format.getGoodsType(rs.getInt("type")));
                    playerEquip.setQuality(Format.getQuality(rs.getInt("quality")));
                    playerEquip.setUseLevel(rs.getInt("use_level"));
                    playerEquip.setDescription(rs.getString("description"));
                    playerEquip.setEquipType(Format.getEquipType(rs.getInt("equip_type")));
                    playerEquip.setLevel(rs.getInt("equip_level"));
                    playerEquip.setBaseProperty((BaseProperty) PropertyData.getBasePropertyById(rs.getInt("base_property")));
                    playerEquip.setSpecialProperty((SpecialProperty) PropertyData.getSpecialPropertyById(rs.getInt("special_property")));
                    playerEquips.add(playerEquip);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return playerEquips;
    }
}
